<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <!--{include file='comm/header.tpl'}-->
    <!--{include file='comm/comm_editor.tpl'}-->
    <title>商品促销标签列表</title>
</head>

<style>
	.fbutton .add {background: url("/framework/flexigrid/css/images/add.png") no-repeat scroll left center rgba(0, 0, 0, 0);}
	 
	div .addVal {
		width:90px;
		margin-bottom:2px;
	    background: url("/framework/flexigrid/css/images/bg.gif") repeat-x scroll center top #EAEAEA;
	    border-color: #CCCCCC #CCCCCC -moz-use-text-color;
	    padding:5px;
	    margin-left:10px;
	    cursor:pointer;
	    border-radius:8px;
	}
</style>

<body>

<div class="container">

    <div class="comm_content">

            <div class="search-box">
                <div style="position:relative">
                    <form id="search_form" onsubmit="return false;" class="form-inline">
                        <span>
                        	促销标签名称：<input type="text" name="tagName" id="search_tagName" class="form-control w150" />
                        </span>
                        <span>
                        	使用状态：
                            <select  class="form-control w150" id="search_useFlg" name="useFlg">
                                <option value="">请选择</option>
                                <option value="1">启用</option>
                                <option value="0">禁用</option>
                            </select>
                        </span>
                        <span>
                        	<input type="button" value="查询" class="btn btn-primary" onclick="goodsPromotion.search();" />
                        </span>
                        <span>
                            <button onclick="goodsPromotion.clearUp();" id="clearBtn" class="btn btn-warning" type="button">重置</button>
                        </span>
                    </form>
                </div>
            </div>

            <table id="flex" style="display:none"></table>

        </div>

</div>

<div id="pop" style="display: none;">

    <form class="form-horizontal" id="editForm" onsubmit="return false;">

        <div class="form-group" id="image1">
            <label for="materName" class="col-sm-3 control-label"><span class="red">*</span> 促销标签名称：</label>
            <div class="col-sm-9">
                <input type="text" class="form-control w300" id="tagName" name="tagName" value="" />
            </div>
        </div>
        
        <div class="form-group" id="image2">
            <label for="sortOrder" class="col-sm-3 control-label"><span class="red">*</span> 排序：</label>
            <div class="col-sm-9">
                <input type="text" class="form-control w150" id="sortOrder" name="sortOrder" value="" />
            </div>
        </div>
        
        <div class="form-group">
            <label for="image" class="col-sm-3 control-label"><span class="red">*</span> 标签图片：</label>
            <div class="col-sm-9">
                <div style="width:100%;">
	                <div style="width:80px;height:80px;">
	                    <img src=""  id="image" style="width:80px;height:80px;" >
	                </div>
	                <div style="padding-top:5px;text-align: left;">
                        <input type="button" class="btn btn-primary" value="上传" id="uploadPic">
	                    <input type="button" class="btn btn-warning" value="删除" onclick="goodsPromotion.deletePic();">&nbsp;
	                    <input type="hidden" name="hidImg" id="hidImg" value="" >
	                </div>
            	</div>
            </div>
        </div>
        
        <input type="hidden" name="tagID" id="tagID" value="" />

    </form>

</div>

<script type="text/javascript">

$("#flex").flexigrid({
    url:"/invokingAjax-backend/goods/goodsPromotion.ajax?operFlg=1",
    dataType: "json",
    colModel : [
            <!--{if isset($edit_permission)}-->
        	{display: "操作", width : 200, align: "center"},
        	<!--{/if}-->
            {display: "序号", name : "sortOrder", width : 100, sortable : true},
            {display: "促销标签名称",width : 300},
            {display: "使用状态", width : 110 , align: "center"}
        ],
        <!--{if isset($edit_permission)}-->
        buttons : [
            {name: "添加", bclass: "add", width : 200, onpress : "goodsPromotion.edit"},
            {separator: true},
            {name: "删除", bclass: "delete", onpress : "goodsPromotion.del.check"}
        ],
        <!--{/if}-->
        sortname: "sortOrder",
        sortorder: "asc",
        usepager: true,
        rp: 10,
        useRp: true,
        showTableToggleBtn: true,
        onSubmit: function() {
            var dt = $('#search_form').serializeArray();
            $("#flex").flexOptions({params: dt});
            return true;
        }
    });

    var goodsPromotion = {

		dt:"",
        //搜索规格
        search: function() {
            var dt = $('#search_form').serializeArray();
            $("#flex").flexOptions({newp: 1,params: dt}).flexReload();
        },
        
		doAction: function( tagID , useFlg ) {
            this.tagID = tagID;
            this.useFlg = useFlg;
            if( useFlg == 0 ) {
                E.confirm("您确认要禁用此商品促销标签吗？", "goodsPromotion.actionConfirm");
            } else {
                E.confirm("您确认要启用此商品促销标签吗？", "goodsPromotion.actionConfirm");
            }

        },
        
        actionConfirm : function () {

        	E.loadding.open('请稍候...');
            $.ajax({
                type: "POST",
                url: "/invokingAjax-backend/goods/goodsPromotion.ajax?operFlg=6",
                dataType: "JSON",
                data : {
                    tagID: this.tagID,
                    useFlg: this.useFlg
                },
                success: function( result_obj ) {
                	E.loadding.close();
                    if (result_obj.code == 200) {
                        E.alert(result_obj.message, 2, "goodsPromotion.load");
                    } else {
                        E.alert(result_obj.message);
                    }
                }
            });
        },

        //编辑或添加弹出框
        edit: function(tagID,imageFlg) {
        	
        	this.imageFlg = imageFlg;

            E.loadding.open('请稍候...');
            var name="";

            if(tagID){
                name="修改商品促销标签";
                E.ajax_post({
                    url: "/invokingAjax-backend/goods/goodsPromotion.ajax?operFlg=4",
                    data:{
                        tagID : tagID
                    },
                    call:"goodsPromotion.val"
                });

            }else{
                name="添加商品促销标签";
                E.ajax_post({
                    url:"/invokingAjax-backend/goods/goodsPromotion.ajax?operFlg=3",
                    call:"goodsPromotion.sortUp"
                });
                
            }

            E.popup.open({
                id:"pop",
                title: name,
                css: "width:760px;",
                btnName: "保存",
                btnCall:"goodsPromotion.check();"
            });
        },
        
      	//最大序列号重置
        sortUp: function(sortOrder){
        	E.loadding.close();
            $("#sortOrder").val(sortOrder);
        },


        //编辑弹出层赋值
        val: function(o){
        	E.loadding.close();
        	
        	if (goodsPromotion.imageFlg == 1) {
        		$('#image1').hide();
            	$('#image2').hide();
        	}
        	
            $("#tagName").val(o.tagName);
            $("#hidImg").val(o.image);
            $("#image").attr('src',o.image);
            $("#sortOrder").val(o.sortOrder);

            //修改时需要
            $("#tagID").val(o.tagID);
        },


        check: function() {
            this.dt = E.getFormValues("editForm");
            var error_msg = "";

            if ($.trim(this.dt.tagName) == "") {
                error_msg += "促销标签名称不能为空<br />";
            }

            if (!E.isInt(this.dt.sortOrder)) {
                error_msg += "排序序号必须为数字<br />";
            }
            
            if (this.dt.hidImg == '') {
            	error_msg += '未上传标签图片<br />';
            }
            
            if (error_msg != "") {
                E.alert(error_msg);
                return false;
            }else {
                goodsPromotion.addSave() ;
            }    
        },

        //保存规格信息
        addSave: function() {
        	
            E.loadding.open("请稍候");
            $tagID = $("#tagID").val();
            
            $.ajax({
                type: "POST",
                url: "/invokingAjax-backend/goods/goodsPromotion.ajax?operFlg=2" ,
                dataType: "JSON",
                data: {
                    tagID		: this.dt.tagID,
                    tagName   : this.dt.tagName,
                    sortOrder   : this.dt.sortOrder,
                    hidImg   	: this.dt.hidImg
                },
                success: function( result_obj ) {
                	
                	E.loadding.close();
                	
                    if (result_obj.code == 200) {
                    	if (result_obj.data == 1) {
                    		E.alert('数据修改成功', 2, "goodsPromotion.addload");
                    	} else {
                    		E.alert('数据保存成功', 2, "goodsPromotion.addload");
                    	}
                        
                    } else {
                        E.alert(result_obj.message);
                    }
                }
            });
        },

        addload : function() {
            window.location.href = 'goodsPromotion.pscript?operFlg=1';
        },

        load: function() {
            $("#flex").flexReload();
        },
        
        clearUp: function(){
        	$('#search_tagName').val('');
        	$('#search_useFlg').val('');
        },

        //删除
        del: {

            p_array: '',

            check: function() {

                var p_obj = $('#flex').find('tr.trSelected');
                if (p_obj.length == 0) {
                    E.alert('请选择要删除的商品促销标签');
                } else {
                    var temp_array = new Array();
                    $.each(p_obj, function(k, v) {
                        var id = $(v).attr('id').replace('row', '');
                        temp_array.push($(v).attr('id').replace('row', ''));
                    });

                    this.p_array = temp_array;
                    E.confirm('您确认要删除选中的商品促销标签吗？', 'goodsPromotion.del.execute');
                }

            },

            execute: function() {
                E.loadding.open('正在删除商品促销标签，请稍候...');
                E.ajax_post({
                    url: '/invokingAjax-backend/goods/goodsPromotion.ajax?operFlg=5',
                    data: {
                        p_array: this.p_array
                    },
                    call: 'goodsPromotion.del.result'
                });
            },

            result: function( o ) {
                E.loadding.close();
                if (o.code == 200) {
                    E.alert('删除成功', 2, 'goodsPromotion.load');
                } else {
                    E.alert(o.message);
                }
            }

        },
        
        //删除商品图片
	    deletePic:function(){
	        $('#image').attr('src', '/quanliadmin/docroot/images/backend/default.jpg');
	        $('#hidImg').val('');
	    },
	
	    //显示图片
	    showImage:function( imageUrl , flg ){
	        $('#image').attr('src', '/quanliadmin/docroot/images/goods/' + imageUrl);
	        $('#hidImg').val(imageUrl);
	    }

    };
    
    KindEditor.ready(function(K) {
        var editor = K.editor({
            allowFileManager : true,
            uploadJson: '/framework/kindeditor/php/upload_json.php'
        });
        
        $(document).on('click','#uploadPic',function(){
            editor.loadPlugin('image', function() {
                editor.plugin.imageDialog({
                    showRemote : false,
                    clickFn : function(url) {
						$('#image').attr('src', url);
			            $('#hidImg').val(url);
                        editor.hideDialog();
                    }
                });
            });
        });
    });

    //给模板展示图片设置默认显示的图片
    (function(){
    	if($('#hidImg').val() == ""){
    		$('#image').attr('src', '/quanliadmin/docroot/images/backend/default.jpg');
    	}
    })();
  
</script>
</body>
</html>